package com.rich.dmr.ui.activity;

import android.annotation.SuppressLint;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.rich.dmr.R;
import com.rich.dmr.ui.view.SimpleToolbar;

import rich.dmr.DmrManager;

public class DmrModeSetActivity extends AppCompatActivity implements View.OnClickListener {

    private String TAG = "rich---DmrModeSetActivity";
    private DmrManager dmrManager;

    private TextView tvShowDmr;
    private TextView tvShowPoc;
    private TextView tvShowDmrRelay;
    private TextView tvShowPocRelay;
    private TextView tvShowRelayIdle;

    @SuppressLint("WrongConstant")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dmr_mode_set);
        dmrManager = (DmrManager)getSystemService("dmrservice");
        SimpleToolbar simpleToolbar = findViewById(R.id.settings_tools_bar);
        simpleToolbar.setLeftTitleClickListener(this);
        simpleToolbar.setLeftTitleDrawable(R.mipmap.nav_back_icon);
        simpleToolbar.setMainTitle(getString(R.string.tools_mode));

        LinearLayout dmrModeSet = findViewById(R.id.dmr_mode_set);
        dmrModeSet.setOnClickListener(this);

        LinearLayout pocModeSet = findViewById(R.id.poc_mode_set);
        pocModeSet.setOnClickListener(this);

        LinearLayout dmrRelayModeSet = findViewById(R.id.dmr_relay_mode_set);
        dmrRelayModeSet.setOnClickListener(this);

        LinearLayout pocRelayModeSet = findViewById(R.id.poc_relay_mode_set);
        pocRelayModeSet.setOnClickListener(this);

        LinearLayout relayIdleModeSet = findViewById(R.id.relay_idle_mode_set);
        relayIdleModeSet.setOnClickListener(this);

        tvShowDmr = findViewById(R.id.show_dmr_mode);
        tvShowPoc = findViewById(R.id.show_poc_mode);
        tvShowDmrRelay = findViewById(R.id.show_dmr_relay_mode);
        tvShowPocRelay = findViewById(R.id.show_poc_relay_mode);
        tvShowRelayIdle = findViewById(R.id.show_relay_idle_mode);

        if(dmrManager != null){
            int currMode = dmrManager.getMode();

            switch (currMode){
                case 0:
                    tvShowDmr.setBackgroundResource(R.drawable.shape_online);
                    break;
                case 1:
                    tvShowPoc.setBackgroundResource(R.drawable.shape_online);
                    break;
                case 2:
                    tvShowDmrRelay.setBackgroundResource(R.drawable.shape_online);
                    break;
                case 3:
                    tvShowPocRelay.setBackgroundResource(R.drawable.shape_online);
                    break;
                case 4:
                    tvShowRelayIdle.setBackgroundResource(R.drawable.shape_online);
                    break;
                default:
                    break;
            }
        }
    }

    @Override
    public void onClick(View view) {
        resetState();
        switch (view.getId()) {
            case R.id.txt_left_title:
                finish();
                break;
            case R.id.dmr_mode_set:
                if(dmrManager != null){
                    dmrManager.setMode(0);
                    tvShowDmr.setBackgroundResource(R.drawable.shape_online);
                }
                break;
            case R.id.poc_mode_set:
                if(dmrManager != null){
                    dmrManager.setMode(1);
                    tvShowPoc.setBackgroundResource(R.drawable.shape_online);
                }
                break;
            case R.id.dmr_relay_mode_set:
                if(dmrManager != null){
                    dmrManager.setMode(2);
                    tvShowDmrRelay.setBackgroundResource(R.drawable.shape_online);
                }
                break;
            case R.id.poc_relay_mode_set:
                if(dmrManager != null){
                    dmrManager.setMode(3);
                    tvShowPocRelay.setBackgroundResource(R.drawable.shape_online);
                }
                break;
            case R.id.relay_idle_mode_set:
                if(dmrManager != null){
                    dmrManager.setMode(4);
                    tvShowRelayIdle.setBackgroundResource(R.drawable.shape_online);
                }
                break;
            default:
                break;
        }
    }

    public void resetState(){
        tvShowDmr.setBackgroundResource(R.drawable.shape_setting_offline);
        tvShowPoc.setBackgroundResource(R.drawable.shape_setting_offline);
        tvShowDmrRelay.setBackgroundResource(R.drawable.shape_setting_offline);
        tvShowPocRelay.setBackgroundResource(R.drawable.shape_setting_offline);
        tvShowRelayIdle.setBackgroundResource(R.drawable.shape_setting_offline);
    }
}